import java.util.Stack;

public class MyStack {
    public static int i = 0;
    public static void stack(String s, Stack<Character> stack) {
        if(stack.empty() && i==s.length())  {
            System.out.println();
            return;
        }

        if(i < s.length()) {
            stack.push(s.charAt(i++));
            if(!stack.empty()) {
                System.out.print(stack.pop() + " ");
            }
            stack(s,stack);
        }
    }

    public static void main(String[] args) {
        Stack<Character> stack = new Stack<>();
        String s = "abcd";
        stack(s,stack);
    }
}
